package org.sky.logger.json;

import org.sky.logger.clazz.Loader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonSerializerLoader extends Loader<JsonSerializer> {
	private final static Logger logger = LoggerFactory.getLogger(JsonSerializerLoader.class);
	private static JsonSerializerLoader loader = new JsonSerializerLoader();

	public static JsonSerializerLoader instance() {
		return loader;
	}

	private JsonSerializerLoader() {
		addClazz("org.codehaus.jackson.map.ObjectMapper", "org.sky.logger.json.support.Jackson1xSerializer");
		addClazz("com.fasterxml.jackson.databind.ObjectMapper", "org.sky.logger.json.support.Jackson2xSerializer");
		addClazz("com.alibaba.fastjson.JSON", "org.sky.logger.json.support.FastjsonSerializer");
		addClazz("com.google.gson.Gson", "org.sky.logger.json.support.GsonJsonSerializer");
	}

	public Class<JsonSerializer> objectType() {
		return JsonSerializer.class;
	}

	/**
	 * 根据当前加载的json框架选择json序列化器
	 * 
	 * @return
	 */
	public static JsonSerializer loadJsonSerializer() {
		JsonSerializer jsonSerializer = instance().getObject();
		if (jsonSerializer != null) {
			logger.info("Found the jsonSerializer is " + jsonSerializer.getClass().getName());
		} else {
			logger.warn("Could not found jsonSerializer! Please add the dependency of jackson or fastjson or gson.");
		}
		return jsonSerializer;
	}

}
